// 文件位置: src/main/java/your/package/controller/OwnerController.java
package your.package.controller;

import your.package.model.Owner;
import your.package.repository.OwnerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/owners")
public class OwnerController {

    @Autowired
    private OwnerRepository ownerRepository;

    @GetMapping("/")
    public List<Owner> getAllOwners() {
        return ownerRepository.findAll();
    }

    @PostMapping("/")
    public Owner createOwner(@RequestBody Owner owner) {
        return ownerRepository.save(owner);
    }

    @PutMapping("/{id}")
    public Owner updateOwner(@PathVariable Long id, @RequestBody Owner ownerDetails) {
        Owner owner = ownerRepository.findById(id)
            .orElseThrow(() -> new RuntimeException("Owner not found on :: " + id));
        owner.setName(ownerDetails.getName());
        owner.setContactNumber(ownerDetails.getContactNumber());
        owner.setEmailAddress(ownerDetails.getEmailAddress());
        return ownerRepository.save(owner);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteOwner(@PathVariable Long id) {
        Owner owner = ownerRepository.findById(id)
            .orElseThrow(() -> new RuntimeException("Owner not found on :: " + id));
        ownerRepository.delete(owner);
        return ResponseEntity.ok().build();
    }
}
